import json
import re
from pathlib import Path

prefix = "/Users/yanyuhui/Desktop/hulog/opmanagement.log.2023-04-24.%s.log"
for i in range(10):
    file_path = prefix % i
    file = Path(file_path)

    exception_number = 0

    if file.exists():
        with file.open() as f:
            result = f.read()
            regex = r"\[({.*})]"
            pattern = re.compile(regex)
            matcher = pattern.search(result)
            while matcher:
                json_str = matcher.group(1)
                if "\"资源池名称\":\"株洲资源池\"" not in json_str:
                    # 在日志中找寻下一个匹配的字符串进行格式化输出
                    matcher = pattern.search(result, matcher.end())
                    continue
                try:
                    json_obj = json.loads(json_str)
                    data = json_obj["data"]
                    json_array = data["data"]
                    for jt in json_array:
                        attrs = jt["attrs"]
                        bmc_ipv4 = attrs.get("BMC地址IPV4", "")
                        description = attrs.get("设备描述", "")
                        zi_type = attrs.get("交换机类型", "")
                        zl = attrs.get("设备子类别", "")
                        ipv4 = attrs.get("管理IPV4", "")
                        ipv6 = attrs.get("管理IPV6", "")
                        ipvdz = attrs.get("管理IP地址", "")
                        management_ip = ipv4 or ipv6 or ipvdz or ""
                        if management_ip in ("10.126.9.137", "10.126.9.132"):
                            print(
                                f"当前的管理IP为:[{management_ip}],具体的数据为:[{json.dumps(attrs, ensure_ascii=False)}]")

                        if "交换机" in description or "交换机" in zi_type or "交换机" in zl or "网关" in zl:
                            continue
                        if not bmc_ipv4:
                            exception_number += 1
                            print(f"当前数据在第:[{data['pageNum']}]页, ipv4为空,数据为:[{attrs}]")
                except Exception as e:
                    print(e)

                matcher = pattern.search(result, matcher.end())

        print(f"当前日志：[{i}] 异常数据量为:[{exception_number}]")
